package org.andao.commons.taglib.ui;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.andao.commons.model.ClassRoomSeat;
import org.andao.commons.taglib.BaseTagSupport;


public class SelectSeatTag extends BaseTagSupport{

    private static final long serialVersionUID = 4542623963816025029L;
    
    private List<ClassRoomSeat> seats;
    
    private String begin;
    
    private String end;
    
    @Override
    public int doEndTag() throws JspException {
        StringBuilder sb = new StringBuilder("");
       //<li class="ui-state-default"><c:out value="${student.studentName}"/> <input id="name" name="name" type="hidden" value="${student.studentName}"></li>
        /*
        for(ClassRoomSeat seat : seats){
            i++;
            sb.append("<li class=\"ui-state-default\">");
            sb.append(seat.getName());
            sb.append("<input id=\"name\" name=\"name\" type=\"hidden\" value='{\"stuId\" : \"" + seat.getStuId() + "\", \"name\" : \""+ seat.getName() + "\"}'>");
            sb.append("</li>");
        }       */
        /*
        for(; i < 100; i++){
            sb.append("<li class=\"ui-state-default\">");
            sb.append("<input id=\"name\" name=\"name\" type=\"hidden\" value='{\"stuId\" : \"\", \"name\" : \"\"}'>");
            sb.append("</li>");
        }*/
        int i = 0;
        int beginInt = Integer.parseInt(begin);
        int endInt = Integer.parseInt(end);
        int length = seats.size();
        if(length>beginInt && length <= endInt){
	        for(i=beginInt;i<length;i++){
	        	ClassRoomSeat seat = seats.get(i);
	        	sb.append("<td type='drag'>");
	        	sb.append(seat.getName());
	        	sb.append("<input name=\"name\" type=\"hidden\" value='{\"stuId\" : \"" + seat.getStuId() + "\", \"name\" : \""+ seat.getName() + "\"}'>");
	        	sb.append("</td>");
	        }
	        for(i=length;i<endInt;i++){
	        	sb.append("<td type='drag'>");
	        	sb.append("<input name=\"name\" type=\"hidden\" value='{\"stuId\" : \"\", \"name\" : \"\"}'>");
	        	sb.append("</td>");
	        }
        }else if(length <= beginInt){
        	for(i=beginInt;i<endInt;i++){
	        	sb.append("<td type='drag'>");
	        	sb.append("<input name=\"name\" type=\"hidden\" value='{\"stuId\" : \"\", \"name\" : \"\"}'>");
	        	sb.append("</td>");
	        }
        }else if(length >endInt){
        	for(i=beginInt;i<endInt;i++){
	        	ClassRoomSeat seat = seats.get(i);
	        	sb.append("<td type='drag'>");
	        	sb.append(seat.getName());
	        	sb.append("<input name=\"name\" type=\"hidden\" value='{\"stuId\" : \"" + seat.getStuId() + "\", \"name\" : \""+ seat.getName() + "\"}'>");
	        	sb.append("</td>");
	        }
        }
        JspWriter writer = this.pageContext.getOut();
        try {
            writer.append(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;
    }

    public List<ClassRoomSeat> getSeats() {
        return seats;
    }

    public void setSeats(List<ClassRoomSeat> seats) {
        this.seats = seats;
    }

	public String getBegin() {
		return begin;
	}

	public void setBegin(String begin) {
		this.begin = begin;
	}

	public String getEnd() {
		return end;
	}

	public void setEnd(String end) {
		this.end = end;
	}
    
    
}
